from typing import List


class Solution:
    def verifyPostorder(self, postorder: List[int]) -> bool:
        def dfs(start, end):
            if start >= end:
                return True
            root = postorder[end]
            left = start
            while postorder[left] < root:
                left += 1
            mid = left
            while postorder[left] > root:
                left += 1
            return left == end and dfs(start, mid-1) and dfs(mid, end - 1)
        return dfs(0, postorder.__len__()-1)